// Title: Triangle program
// Author: Luis Beyk.
// Course: SSW567

package mypackage;

import java.util.Arrays;

public class Triangle
{
	private boolean validInputs(double[] fInput)
	{
		if (fInput[0] > 0.0 && 
			fInput[1] > 0.0 &&
			fInput[2] > 0.0 &&
			(fInput[0] < (fInput[1] + fInput[2])) &&
			(fInput[1] < (fInput[2] + fInput[0])) &&
			(fInput[2] < (fInput[0] + fInput[1])))
			return true;
		else
			return false;
	}
	
	private boolean isRightTriangle(double[] fLenghts)
	{
		double fSumLegsSquared = (fLenghts[0] * fLenghts[0]) + 
				                 (fLenghts[1] * fLenghts[1]);
		double fHypotenuseSquared = (fLenghts[2] * fLenghts[2]);
		double fDeltaError = 0.001;
		if (Math.abs(fSumLegsSquared - fHypotenuseSquared)/fHypotenuseSquared
				< fDeltaError)
			return true;
		else
			return false;
	}
	
	public String determineType(double[] fLenghts)
	{
		String sTriangleType = "";
		Arrays.sort(fLenghts); 
		// fLenghts is a non-decreasing list of doubles
		double fThreshold = 0.0001;
		if (validInputs(fLenghts))
		{
			if ((fLenghts[1] - fLenghts[0]) > fThreshold)
			{
				// fLenghts[0] < fLenghts[1]
				if ((fLenghts[2] - fLenghts[1]) > fThreshold)
				{
					// fLenghts[0] < fLengths[1] < fLenghts[2] => scalene
					if (isRightTriangle(fLenghts))
						sTriangleType = "right scalene triangle";
					else
						sTriangleType = "scalene triangle";
				}
				else // fLengths[1] == fLenghts[2]
				{
					// fLenghts[0] < fLengths[1] == fLenghts[2] => isosceles
					sTriangleType = "isosceles triangle";
				}
			}
			else // fLengths[0] == fLenghts[1]
			{
				if ((fLenghts[2] - fLenghts[1]) > fThreshold)
				{
					// fLenghts[0] == fLengths[1] < fLenghts[2] => isosceles
					if (isRightTriangle(fLenghts))
						sTriangleType = "right isosceles triangle";
					else
						sTriangleType = "isosceles triangle";
				}
				else // fLengths[1] == fLenghts[2]
				{
					// fLenghts[0] == fLengths[1] == fLenghts[2] => equilateral
					sTriangleType = "equilateral triangle";
				}
			}
		}
		else
		{
			sTriangleType = "invalid triangle";
		}
		return sTriangleType;
	}
}
